package com.bifrost.validator;

import org.springframework.stereotype.Component;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 数字类型参数校验：如 type = 1/2/3
 *
 * @author rangf
 * @date 2017/11/18
 */
@Component
public class ArgsValidatorImpl implements ConstraintValidator<ArgsValidator, Object> {

    private String values;

    @Override
    public void initialize(ArgsValidator digitalValidator) {
        this.values = digitalValidator.values();
    }

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        boolean flag = false;
        String[] valueArray = values.split(",");
        for (int i = 0; i < valueArray.length; i++) {
            if (valueArray[i].equals(o)) {
                flag = true;
                break;
            }
        }
        return flag;
    }
}
